//
//  CZBaseTableViewController.swift
//  SinaWeibo
//
//  Created by 浩记 on 16/3/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


//作为每一个tabBar控制器的父类
class CZBaseTableViewController: UITableViewController {
    //用户是否登录
    var userLogin : Bool = false
    
    override func loadView() {
        
        //第一次加载时图的时候，判断如果登录了，那就加载这个表格视图(tableView)，没登录的话就加载访客时图
        userLogin ? super.loadView() : setUpVistorView()
        
        //准备闭包，一段等待执行的代码--点击注册闭包
        vistorView.clickRegisterClosrue = {
            
            let vc = UIViewController()
            
            vc.view.backgroundColor = UIColor.yellowColor()
            vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "完成", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
            vc.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
            //点击注册就present一个控制器视图
           self.presentViewController(UINavigationController(rootViewController: vc), animated: true, completion: nil)
        }
        //准备闭包，一段等待执行的代码--点击登录闭包
        vistorView.clickLoginClosrue = {
            let vc = CZOauthController()
//            vc.view.backgroundColor = UIColor.cyanColor()
//            vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "")
//            vc.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
            //点击登录就present一个授权控制器视图
            self.presentViewController(UINavigationController(rootViewController: vc), animated: true, completion: nil)
        }
        
    }
    
    func close(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    //设置访客视图
    private func setUpVistorView () {
        
        //加载访客视图
        view = vistorView
        
        //根据不同的控制器来设置每一个控制器的访客视图上的子控件等属性
        if (self is CZHomeViewController) {//首页控制器
           
            //设置首页控制器视图的图片和消息Label
            vistorView.setVistor("visitordiscover_feed_image_house", LaBelString: "关注一些人,看看有上面惊喜")
            
            //显示转轮
            vistorView.iconView.hidden = false
            
            //开始转轮动画
            vistorView.startRotation()
            
            //把遮盖视图带到最前面--会遮住所有视图
//            vistorView.bringSubviewToFront(vistorView.coverView)
            //把遮盖视图插入某个视图的下面，那就不会遮住要现实的视图
            vistorView.insertSubview(vistorView.coverView, belowSubview: vistorView.homeView)
            
        }else if (self is CZDiscoverViewController) {//发现控制器
            
            //设置发现控制器视图的图片和消息Label
            vistorView.setVistor("visitordiscover_image_message", LaBelString: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        }else if (self is CZMessageViewController) {//消息控制器
            
            //设置消息控制器视图的图片和消息Label
            vistorView.setVistor("visitordiscover_image_message", LaBelString: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        }else if (self is CZProfileViewController) {//”我“控制器
            
            //设置”我“控制器视图的图片和消息Label
            vistorView.setVistor("visitordiscover_image_profile", LaBelString: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
        
        //导航栏左右BarButtonItem
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "clickRegisteLeftBarButtonItem")
        //导航栏左右BarButtonItem
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "clickLoginRightBarButtonItem")
    }
    
    //MARK:- 点击导航栏按钮事件
    ///点击导航栏左边注册
    func clickRegisteLeftBarButtonItem() {
        vistorView.didClickRegisterButton()
    }
    ///点击导航栏右边登录
    func clickLoginRightBarButtonItem() {
        vistorView.didClickLoginButton()
    }
    
    //懒加载
    private lazy var vistorView : CZVistorView = CZVistorView()

}
